/*
 * The SyslogCollector will accept syslog packets on a UDP port configured in a 
 * config file.  This collector can be configured to store the syslog events 
 * in any of the following places: local memory buffer, local file, send over a 
 * TCP connection to a Normalizer.
 */
package NetworkCollectors;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Craig Campbell
 */
//public class SyslogCollector implements Runnable {
public class SyslogCollector extends Thread {
    
    int port = 514;
    boolean shouldRun = true;
    
    // SyslogCollector that accepts the default settings.
    public SyslogCollector() {}
    
    // SyslogCollector that specifies a port to listen on.
    public SyslogCollector(int p) {
        port = p;
    }

    @Override
    public void run() {
        try {
            //throw new UnsupportedOperationException("Not supported yet."); 
            //DatagramSocket socket = new DatagramSocket();
            DatagramSocket socket = new DatagramSocket(new InetSocketAddress(Inet4Address.getLocalHost(), 61616));
            socket.setSoTimeout(0);
            DatagramPacket packet = new DatagramPacket(new byte[512], 512);
            System.out.println("Waiting for a UDP packet.");
            socket.receive(packet);
            System.out.println("Should have the packet now.");
            System.out.println(packet.getData().toString());
        } catch (SocketException ex) {
            Logger.getLogger(SyslogCollector.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SyslogCollector.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void end() {
        shouldRun = false;
    }
    
    public void printDetails() {
        System.out.println("<---- Syslog Collector Details ---->");
        System.out.println("<");
        System.out.println("< Listening Port: " + port);
        System.out.println("<");
        System.out.println("<-- End Syslog Collector Details -->");
    }
    
    public static void main(String args[]) {
        //SyslogCollector sc = new SyslogCollector();
        SyslogCollector sc = new SyslogCollector(61616);
        sc.printDetails();
        //sc.run();
        sc.start();
    }
    
}

class SyslogProcessor extends Thread {
    
    public SyslogProcessor() {
        
    }

    @Override
    public void run() {
        //throw new UnsupportedOperationException("Not supported yet."); 
    }
}
